package com.eurake.seckill.vo;

import com.eurake.seckill.utils.ValidatorUtil;
import com.eurake.seckill.validator.IsMobile;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class IsMobileValidator implements ConstraintValidator<IsMobile,String>{

    private boolean required;
    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(required){
            return ValidatorUtil.isMobile(s);
        }else{
            if(s==null){
                return true;
            }
            return ValidatorUtil.isMobile(s);
        }
    }
}
